#!/usr/bin/env python

"""
*****************************************************************
libsms.py
This file is part of s60console project.

Released under GNU GPLv2.
See accompanying gpl-2.0.txt file
or visit http://www.gnu.org/licenses/gpl-2.0.txt

Author: Kashif Iftikhar (kashif [at] compulife.com.pk)

Description:
------------
Contains functions related to SMS sending and receiving

*****************************************************************
"""

import bluetooth

#00:11:67:00:00:00
MOBILE_SERVER_PORT = 21

def dict2xml(d):
    "convert a dictionary object to xml"
    ret = "<cmdinfo>\n"
    for k in d:
        ret += "<%s>%s</%s>" % (k.upper(), d[k], k.upper())
    ret += "</cmdinfo>\n"
    return ret

def sendsms(bt_addr, cmd_info):
    sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
    
    target=(bt_addr,MOBILE_SERVER_PORT)
    
    print "Connecting to "+str(target),
    sock.connect(target)
    print " ... OK.\n Sending SMS ... ",
    print repr(cmd_info)
    sock.send(dict2xml(cmd_info))
    print "DONE"
    sock.close()

def _tests():
    "Test cases for the module"
    pass

if __name__ == '__main__':
    import sys
    if len(sys.argv)>1 and 'run_tests'==sys.argv[1]:
        _tests()
    
    sendsms('00:19:B7:3E:DC:FD', {'message': 'Ab msg aya?', 'cmd': 'SENDSMS', 'contact': '03437158780'})
    